import 'package:mmkv/mmkv.dart';
import 'package:flutter_syt/starter/storage/storage.dart';

class MMKVStorage with Storage {
  MMKV? _mmkv;

  MMKVStorage._() {
    _mmkv = MMKV.defaultMMKV();
  }

  static Future<MMKVStorage> initialize() async {
    await MMKV.initialize();
    return MMKVStorage._();
  }

  MMKV get _mmkvObj => _mmkv!;

  @override
  List<String> get allKeys => _mmkvObj.allKeys;

  @override
  void clearAll() {
    _mmkvObj.clearAll();
  }

  @override
  void close() {
    _mmkvObj.close();
  }

  @override
  bool containsKey(String key) {
    return _mmkvObj.containsKey(key);
  }

  @override
  bool getBool(String key, {bool defaultValue = false}) {
    return _mmkvObj.decodeBool(key, defaultValue: defaultValue);
  }

  @override
  double getDouble(String key, {double defaultValue = 0}) {
    return _mmkvObj.decodeDouble(key, defaultValue: defaultValue);
  }

  @override
  int getInt(String key, {int defaultValue = 0}) {
    return _mmkvObj.decodeInt(key, defaultValue: defaultValue);
  }

  @override
  int getInt32(String key, {int defaultValue = 0}) {
    return _mmkvObj.decodeInt32(key, defaultValue: defaultValue);
  }

  @override
  String? getString(String key) {
    return _mmkvObj.decodeString(key);
  }

  @override
  void removeValue(String key) {
    _mmkvObj.removeValue(key);
  }

  @override
  void removeValues(List<String> keys) {
    _mmkvObj.removeValues(keys);
  }

  @override
  bool setBool(String key, bool value) {
    return _mmkvObj.encodeBool(key, value);
  }

  @override
  bool setDouble(String key, double value) {
    return _mmkvObj.encodeDouble(key, value);
  }

  @override
  bool setInt(String key, int value) {
    return _mmkvObj.encodeInt(key, value);
  }

  @override
  bool setInt32(String key, int value) {
    return _mmkvObj.encodeInt32(key, value);
  }

  @override
  bool setString(String key, String? value) {
    return _mmkvObj.encodeString(key, value);
  }
}
